﻿#if NET35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kernelx.Common.DataAnnotations
{
    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
    public class RequiredAttribute : ValidationAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        public bool AllowEmptyStrings { get; set; }


        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null || (!AllowEmptyStrings && string.IsNullOrEmpty(value.ToString())))
                return new ValidationResult("为必填项,不能为空");
            return null;
        }
    }
}

#endif